package com.abiao.shop.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: MQSender   消息发送者（生产者）(学习记录，与项目无关)
 * @Author: AB
 * @Date: 2021-07-21
 * @Version:v1.0
 */
@Slf4j
@Service
public class MQSender {

    @Autowired
    AmqpTemplate amqpTemplate;

    //direct
    public void send(Object msg){
        log.info("MQSender: "+msg.toString());
        amqpTemplate.convertAndSend(MQConfig.QUEUE_NAME,msg);
    }

    //topic
    public void sendTopic(Object msg){
        log.info("MQSenderTopic: "+msg.toString());
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",msg+"(key:topic.key1)");
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.y",msg+"(key:topic.y)");
    }

    //fanout
    public void sendFanout(Object msg){
        log.info("MQSenderFanout: "+msg.toString());
        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",msg);
    }

    //header
    public void sendHeader(String msg){
        log.info("MQSenderHeader: "+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("key1","value1");
        properties.setHeader("key2","value2");
        Message message = new Message(msg.getBytes(),properties);
        amqpTemplate.convertAndSend(MQConfig.HEADER_EXCHANGE,"",message);
    }
}
